clear all
set more off
cd "S:\Project\DemoSos2\common\felles\JR_RG\DrVA\CleanData\"


**------------------------------------------------------------------------------*
** GP and ER cons. 
*------------------------------------------------------------------------------*
forvalues i = 2006/2020       {
	use "S:/Project/DemoSos2/data2020/KUHR/KUHR`i'", clear
	drop if lopenr == ""
	 
	** Keep only GP cons.
	rename (Diagnose BehandlerID BehKnr BehPostnr Refusjon Alder Tid kjonn) (diagnose docid gpmun gppostcode reimburse age hour gender)
	
	ge simple_contact = regexm(Takst, "1ADx") | regexm(Takst, "1AKx") | regexm(Takst, "1BDx") | regexm(Takst, "1BKx") | regexm(Takst, "1Ex") | regexm(Takst, "1Fx") | regexm(Takst, "1Gx") | regexm(Takst, "1Hx") | regexm(Takst, "1Ix")  
	
	ge simple_inperson = regexm(Takst, "1ADx") | regexm(Takst, "1AKx") | regexm(Takst, "1BDx") | regexm(Takst, "1BKx") | regexm(Takst, "1Ex") 
	
	ge simple_nopinperson = regexm(Takst, "1Fx") | regexm(Takst, "1Gx") | regexm(Takst, "1Hx") | regexm(Takst, "1Ix")   
	
	ge consult =  regexm(Takst, "2ADx") | regexm(Takst, "2AEx") | regexm(Takst, "2AKx") | regexm(Takst, "2BDx") | regexm(Takst, "2CDx") | regexm(Takst, "2CKx") | regexm(Takst, "2DDx") | regexm(Takst, "2DKx") | regexm(Takst, "2HDx") | regexm(Takst, "2LDx") | regexm(Takst, "2Px") | regexm(Takst, "2KNx") 
	
	ge sick_visit  = regexm(Takst, "11") | regexm(Takst, "14") | regexm(Takst, "15") 
	
	ge procedure =(regexm(Takst, "100") | regexm(Takst, "101") | regexm(Takst, "102") | regexm(Takst, "103") | regexm(Takst, "104") | regexm(Takst, "105") | regexm(Takst, "106") | regexm(Takst, "107") | regexm(Takst, "108")| regexm(Takst, "109") | regexm(Takst, "110") | regexm(Takst, "111")) 
	
	ge life_style_adv = regexm(Takst, "101") | regexm(Takst, "102")
	
	ge major_surg = regexm(Takst, "105") | regexm(Takst, "106")
	
	ge gy_exam =  regexm(Takst, "103b") | regexm(Takst, "104") |  regexm(Takst, "214")  | regexm(Takst, "215") | regexm(Takst, "217")
	
	ge ultra_sound = regexm(Takst, "108") 
	
	ge blood_test = regexm(Takst, "1Ex")
	
	ge labtest = (regexm(Takst, "701") | regexm(Takst, "702") | regexm(Takst, "703") | regexm(Takst, "704") | regexm(Takst, "705") | regexm(Takst, "706") | regexm(Takst, "707") | regexm(Takst, "708") | regexm(Takst, "709")| regexm(Takst, "710") | regexm(Takst, "711") | regexm(Takst, "712")) 
	
	ge spec = regexm(Takst, "3ADx") | regexm(Takst, "3BDx") | regexm(Takst, "3Cx")|regexm(Takst, "4Ax") | regexm(Takst, "3Bx") | regexm(Takst, "3Cx")| regexm(Takst, "4Ax")| regexm(Takst, "4Bx")| regexm(Takst, "4Cx")| regexm(Takst, "4Dx")            
		
	ge small_procedure =regexm(Takst, "100") | regexm(Takst, "101") | regexm(Takst, "102") | regexm(Takst, "103") | regexm(Takst, "104") 
	
	ge large_procedure = regexm(Takst, "105") | regexm(Takst, "106") | regexm(Takst, "107") | regexm(Takst, "108")| regexm(Takst, "109") | regexm(Takst, "110") | regexm(Takst, "111") 
	
	ge appointment = simple_contact == 1 | consult==1 
	
 
	**  Count number of GP cons., ER cons., and number of sick notes each year
	ge sl = (regexm(Takst, "L1x1")) 

	*-----------------------------------------------------------------------------*
	** Psych. symp. and diag.
	* Psych. all
	ge psych = (regexm(diagnose , "P")) if TypeDiag == "ICPC-2"
	ge sl_p = (regexm(Takst, "L1x1")) if psych==1

	* Psych symptoms (P00-P29)
	ge ps = (regexm(diagnose , "P0")|regexm(diagnose , "P1")|regexm(diagnose , "P2")) if TypeDiag == "ICPC-2"
	* Psych. diagnoses (P70-P99)
	ge pd = (regexm(diagnose, "P7")|regexm(diagnose, "P8")|regexm(diagnose, "P9")) if TypeDiag == "ICPC-2"
	
	*diagnoses
	foreach x in A B D E F H K L N P R S T U W X Y Z {				
		ge diag_`x' = (regexm(diagnose , "`x'")) if TypeDiag == "ICPC-2"
	}
	
	foreach x in P0 P1 P2 P7 P8 P9 {
			ge psyc_`x' = (regexm(diagnose , "`x'")) if TypeDiag == "ICPC-2"
	}
	
	ge depression =  (regexm(diagnose , "P03")|regexm(diagnose , "P76")|regexm(diagnose , "P77")) if TypeDiag == "ICPC-2"
	ge anxiety =  (regexm(diagnose , "P01")|regexm(diagnose , "P02")|regexm(diagnose , "P06")|regexm(diagnose , "P74")) if TypeDiag == "ICPC-2"
	ge substance = (regexm(diagnose , "P15")|regexm(diagnose , "P16")|regexm(diagnose , "P17")|regexm(diagnose , "P18")|regexm(diagnose , "P19")) if TypeDiag == "ICPC-2"
	ge other_psy =  psych == 1 & depression!=1 & anxiety!=1 & substance !=1
	*-----------------------------------------------------------------------------*
	** Rest category: All other diag.
	ge rest = psych!=1 & TypeDiag == "ICPC-2"
	ge gp_notpreg =  diag_W!=1 & TypeDiag == "ICPC-2"
	
	ge contracep = (regexm(diagnose , "W10")|regexm(diagnose , "W11")|regexm(diagnose , "W12")|regexm(diagnose ,"W13")|regexm(diagnose,"W14")) if diag_W==1 & TypeDiag == "ICPC-2"
	
	ge preg = (regexm(diagnose, "W78")|regexm(diagnose, "W79")|regexm(diagnose, "W80")|regexm(diagnose, "W81")|regexm(diagnose, "W82")|regexm(diagnose, "W83")|regexm(diagnose, "W84")|regexm(diagnose, "W85")|regexm(diagnose, "W90")|regexm(diagnose, "W91")|regexm(diagnose, "W92")|regexm(diagnose, "W93")|regexm(diagnose, "W94")|regexm(diagnose, "W95")|regexm(diagnose, "W96")|regexm(diagnose, "W99")) if diag_W==1 & TypeDiag == "ICPC-2"
	ge gest_diabetes = regexm(diagnose, "W85")
	
	ge preg_other = contracep!=1 & preg!=1 if diag_W==1 & TypeDiag == "ICPC-2"
	
	ge gest_hypertension = regexm(diagnose, "K86") |regexm(diagnose, "K87") if diag_W==1 & TypeDiag == "ICPC-2"
	
	ge icd10_l = substr(diagnose ,1,1) if TypeDiag == "ICD-10"
	
	*chronic
	ge cancer = regexm(diagnose, "A79") | ///
			regexm(diagnose, "B72") |regexm(diagnose, "B73") | regexm(diagnose, "B74") |regexm(diagnose, "B75") | ///	
			regexm(diagnose, "D74") |regexm(diagnose, "D75") | regexm(diagnose, "D76") | regexm(diagnose, "D77") | regexm(diagnose, "D78") | /// 
			regexm(diagnose, "F74") | /// 
			regexm(diagnose, "H75") | ///
			regexm(diagnose, "K72") | ///
			regexm(diagnose, "L71") | regexm(diagnose, "L97") | ///
			regexm(diagnose, "N74") |regexm(diagnose, "N75") | regexm(diagnose, "N76") | ///
			regexm(diagnose, "R84") |regexm(diagnose, "R85") | regexm(diagnose, "R86") | ///
			regexm(diagnose, "S77") |regexm(diagnose, "S78") | regexm(diagnose, "S79") | regexm(diagnose, "S80") | ///
			regexm(diagnose, "T71") |regexm(diagnose, "T72") | regexm(diagnose, "T73") | ///
			regexm(diagnose, "S77") |regexm(diagnose, "S78") | regexm(diagnose, "S79") | regexm(diagnose, "S80") | ///
			regexm(diagnose, "U75") |regexm(diagnose, "U76") | regexm(diagnose, "U77") | regexm(diagnose, "U78") | regexm(diagnose, "U79") | ///
			regexm(diagnose, "W72") |regexm(diagnose, "W73") | ///
			regexm(diagnose, "X75") |regexm(diagnose, "X76") | regexm(diagnose, "X77") | regexm(diagnose, "X78") | regexm(diagnose, "X79") |  regexm(diagnose, "X80") |  regexm(diagnose, "X81") | ///
			regexm(diagnose, "Y77") |regexm(diagnose, "Y78") | regexm(diagnose, "Y79")  if TypeDiag == "ICPC-2"
	
	ge digest_chronic = regexm(diagnose, "D94") | regexm(diagnose, "D98")  if TypeDiag == "ICPC-2"

	ge heart_chronic = regexm(diagnose, "K74") | regexm(diagnose, "K75") | regexm(diagnose, "K76") | regexm(diagnose, "K77") | ///
			regexm(diagnose, "K78") | regexm(diagnose, "K79") |  regexm(diagnose, "K80") |  regexm(diagnose, "K81") | ///
			regexm(diagnose, "K82") | regexm(diagnose, "K83") |  regexm(diagnose, "K84") |  regexm(diagnose, "K85") | ///
			regexm(diagnose, "K86") | regexm(diagnose, "K87") |  regexm(diagnose, "K88") |  regexm(diagnose, "K89") | ///
			regexm(diagnose, "K90") | regexm(diagnose, "K91") |  regexm(diagnose, "K92") |  regexm(diagnose, "K93") | ///
			regexm(diagnose, "K94") | regexm(diagnose, "K95") |  regexm(diagnose, "K96") |  regexm(diagnose, "K99") if TypeDiag == "ICPC-2" 
			
	ge endoc_chronic = regexm(diagnose, "T81") |regexm(diagnose, "T82") | regexm(diagnose, "T83") | regexm(diagnose, "T84") | regexm(diagnose, "T85") | ///
			regexm(diagnose, "T86") |regexm(diagnose, "T87") | regexm(diagnose, "T88") | regexm(diagnose, "T89") | regexm(diagnose, "T90") | ///
			regexm(diagnose, "T91") |regexm(diagnose, "T92") | regexm(diagnose, "T93") | regexm(diagnose, "T99") if TypeDiag == "ICPC-2"
	
	ge any_cardiogp = regexm(diagnose, "K") if TypeDiag == "ICPC-2"
	
	*-----------------------------------------------------------------------------*
	* processes
	*-----------------------------------------------------------------------------*
	ge diag_test = regexm(diagnose, "30") | regexm(diagnose, "31") | regexm(diagnose, "32") | regexm(diagnose, "33") | regexm(diagnose, "34")  ///
		| regexm(diagnose, "35") | regexm(diagnose, "36") | regexm(diagnose, "37") | regexm(diagnose, "38") | regexm(diagnose, "39") ///
		| regexm(diagnose, "40") | regexm(diagnose, "41") | regexm(diagnose, "42") | regexm(diagnose, "43") | regexm(diagnose, "44") ///
		| regexm(diagnose, "45") | regexm(diagnose, "46") | regexm(diagnose, "47") | regexm(diagnose, "48") | regexm(diagnose, "49") if TypeDiag == "ICPC-2"
	ge prescription = regexm(diagnose, "50") if TypeDiag == "ICPC-2"
	ge referral = regexm(diagnose, "64") | regexm(diagnose, "65") | regexm(diagnose, "66") | regexm(diagnose, "67") | regexm(diagnose, "68") | regexm(diagnose, "69") if TypeDiag == "ICPC-2"	
	ge other = 	diag_test == 0 & prescription == 0 & referral == 0 if TypeDiag == "ICPC-2"	 
	
	destring reimburse, replace dpcomma
	
	ge year = year(KUHRDato)
	ge month = month(KUHRDato)
	qui compress
	save "kurh_`i'.dta", replace
}


* for individuals: gp
forvalues i=2006/2020 {
	use "kurh_`i'.dta", clear
	drop if lopenr == ""

	keep if TypePraksis == "Fastlege"  | TypePraksis == "Fastlønnet"  
	
	keep age simple_contact  simple_inperson simple_nopinperson consult sick_visit procedure life_style_adv major_surg ultra_sound ///
		blood_test labtest spec small_procedure large_procedure appointment sl psych sl_p ps pd rest ///
		depression anxiety substance other_psy diag_A diag_B diag_D diag_E diag_F diag_H diag_K diag_L diag_N diag_P ///
		diag_R diag_S diag_T diag_U diag_W diag_X diag_Y diag_Z psyc_P0 psyc_P1 psyc_P2 psyc_P7 psyc_P8 psyc_P9  ///
		reimburse ///
		cancer digest_chronic heart_chronic endoc_chronic any_cardiogp diag_test prescription referral other lopenr year 
	ge total = 1
	collapse (mean) age (sum) total simple_contact  simple_inperson simple_nopinperson consult sick_visit procedure life_style_adv major_surg ultra_sound ///
		blood_test labtest spec small_procedure large_procedure appointment sl psych sl_p ps pd rest ///
		depression anxiety substance other_psy diag_A diag_B diag_D diag_E diag_F diag_H diag_K diag_L diag_N diag_P ///
		diag_R diag_S diag_T diag_U diag_W diag_X diag_Y diag_Z psyc_P0 psyc_P1 psyc_P2 psyc_P7 psyc_P8 psyc_P9  ///
		reimburse ///
		cancer digest_chronic heart_chronic endoc_chronic any_cardiogp diag_test prescription referral, by(lopenr year)
	qui compress
	save "khurh_annual`i'.dta", replace
}

use "khurh_annual2006.dta", clear
forvalues i=2007/2020 {
	append using "khurh_annual`i'.dta"
}
compress
save khurh_annual, replace


* for individuals: er
forvalues i=2006/2020 {
	use "kurh_`i'.dta", clear
	drop if lopenr == ""

	keep if TypePraksis == "Legevakt"  | TypePraksis == "Legevakt kommunal" 
	
	keep reimburse lopenr year 
	ge er = 1
	collapse (sum) er reimburse, by(lopenr year)
	save "khurh_er`i'.dta", replace
}

use "khurh_er2006.dta", clear
forvalues i=2007/2020 {
	append using "khurh_er`i'.dta"
}
compress
save khurh_er, replace




*for doc
forvalues i=2006/2020 {
	use simple_contact simple_contact  simple_nopinperson consult sick_visit procedure life_style_adv ///
		blood_test labtest  appointment sl reimburse ///
		diag_test prescription referral lopenr year docid TypePraksis if TypePraksis == "Fastlege"  | TypePraksis == "Fastlønnet" using "kurh_`i'.dta", clear
	drop if lopenr == ""

	ge total = 1
	bys lopenr: ge n=_n
	ge tot_patients = 1 if n == 1
 	collapse (mean) reimburse tot_patients total simple_contact  simple_nopinperson consult sick_visit procedure life_style_adv ///
		blood_test labtest  appointment sl diag_test prescription referral , by(docid year)
		
	save "khurh_gp`i'.dta", replace
}


use khurh_gp2006, clear
forvalues i=2007/2020 {
	append using "khurh_gp`i'.dta"
}
collapse reimburse tot_patients total  simple_contact  simple_nopinperson consult sick_visit procedure life_style_adv ///
		blood_test labtest  sl diag_test prescription referral, by(docid)
rename docid str_exogGPIDnew
for var total simple_contact  simple_nopinperson  consult procedure life_style_adv blood_test labtest sl diag_test prescription referral reimburse: replace X=X/tot_patients 
drop tot_patients 
save "khurh_gp_info.dta", replace


*------------------------------------------------------------------------------*
** REFERRALS FROM GP TO HOSPITAL (ONLY PLANNED HOSP ADMISSIONS) **
*------------------------------------------------------------------------------*	
*------------------------------------------------------------------------------*
** SOMATIC **
*------------------------------------------------------------------------------*
foreach num of numlist  2008/2020 {
	use aar lopenr innuke innmatehast using "$source\SOM\somatikk`num'.dta", clear
	drop if lopenr == ""
	cap replace innuke = "" if innuke=="7gesund"
	keep if innmatehast == "2"
	destring _all, replace
	save hosp_temp`num', replace
}
use hosp_temp2008, clear
forvalues i=2009/2020 {
	append using hosp_temp`i'
}
for num 2008/2020: erase hosp_tempX.dta
rename aar year
drop if innuke==.
save hosp_temp, replace

	
*------------------------------------------------------------------------------*
** GP **
*------------------------------------------------------------------------------*	
foreach num of numlist 2006/2020 {
	use lopenr docid age KUHRDato TypePraksis using "kurhgp_`num'.dta", clear
	keep if TypePraksis == "Fastlege"  | TypePraksis == "Fastlønnet" 
	rename doc gpid
	save tempref`num', replace
}

use tempref2006, clear
forvalues i=2007/2020 {
	append using "tempref`i'.dta"
}
for num 2006/2020: erase "temprefX.dta"

ge week_gp = week(KUHRDato)
drop if week_gp==.
ge year = year(KUHRDato)

merge m:m lopenr year using hosp_temp
keep if _m!=1
drop if week_gp==.
sort lopenr KUHRDato
ge diff = innuke-week_gp
keep if diff > 0
su diff, d
ge nb_ref_short = diff>0 & diff <= 4
ge nb_ref = diff>0 

preserve
* by gp
	collapse (mean) diff (sum) nb_ref nb_ref_short, by(gpid year)
	rename diff ref_time
	save referral_gp, replace
	
restore
* for ind
collapse diff, by(lopenr year)
save referral_ind, replace

erase hosp_temp.dta



